package com.blkbox.javaee.jms.weblogic;

import java.util.Hashtable;

import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class JMSReceiver {

	public static void main(String[] args) {
		System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
		System.setProperty(Context.PROVIDER_URL, "t3://localhost:7001");
		QueueConnection con = null;
		QueueSession session = null;
		QueueReceiver reciver = null;
		Context ctx = null;

		try {
			ctx = new InitialContext();
			// step 1. look up a QueueConnectionFactory
			QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("jms/ConnectionFactoryTest");
			// step 2. create a QueueConnection
			con = factory.createQueueConnection();
			// step 3. create a QueueSession
			session = con.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
			// step 4. look up a Queue
			Queue queue = (Queue) ctx.lookup("jms/QueueTest");
			// step 5. create a QueueReceiver
			QueueReceiver receiver = session.createReceiver(queue);
			// step 6. call start() on con
			con.start();
			// step 7. call receive() on receiver to get message from the JMS Server
			while(true){
				TextMessage message = (TextMessage) receiver.receive(100000);
				if (null != message) {
					System.out.println("message is" + message.getText());
				} else {
					break;
				}
			}
		} catch (NamingException ne) {
			ne.printStackTrace();
		} catch (JMSException jse) {
			jse.printStackTrace();
		} finally {
			if (reciver != null)
				try {
					reciver.close();
				} catch (JMSException e) {
				}
			if (session != null)
				try {
					session.close();
				} catch (JMSException e) {
				}
			if (con != null)
				try {
					con.close();
				} catch (JMSException e) {
				}
			if (ctx != null)
				try {
					ctx.close();
				} catch (NamingException e) {
				}
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void main1(String[] args) throws NamingException, JMSException {
		// init JNDI context
		String JNDIFactory = "weblogic.jndi.WLInitialContextFactory";// define
																		// JNDI
																		// context
																		// factory
		String providerUrl = "t3://localhost:7001"; // define weblogic JMS url
		Hashtable env = new Hashtable();
		env.put(Context.INITIAL_CONTEXT_FACTORY, JNDIFactory);
		env.put(Context.PROVIDER_URL, providerUrl);
		Context ctx = new InitialContext(env);

		// find connection factory
		String connFactoryJNDI = "jms/ConnectionFactoryTest"; // jms
																// connectionFactory
																// JNDI name
		QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup(connFactoryJNDI);
		// create queue connection
		QueueConnection qConn = (QueueConnection) connFactory.createConnection();
		// create session
		QueueSession qSession = qConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
		// find queue by JNDI lookup
		Queue queue = (Queue) ctx.lookup("jms/QueueTest");

		// create receiver
		QueueReceiver qReceiver = qSession.createReceiver(queue);
		// create message listener
		qConn.start();
		qReceiver.setMessageListener(new MessageListener() {

			public void onMessage(Message msg) {
				String msgText = "";
				double d = 0;
				try {
					if (msg instanceof TextMessage) {
						msgText = ((TextMessage) msg).getText();
					} else if (msg instanceof StreamMessage) {
						msgText = ((StreamMessage) msg).readString();
						d = ((StreamMessage) msg).readDouble();
					} else if (msg instanceof BytesMessage) {
						byte[] block = new byte[1024];
						((BytesMessage) msg).readBytes(block);
						msgText = String.valueOf(block);
					} else if (msg instanceof MapMessage) {
						msgText = ((MapMessage) msg).getString("name");
					}
				} catch (JMSException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				System.out.println(msgText + " " + d);
			}
		});

	}

}